// 使用内置 API
var reverseWords1 = function(s) {
    return s.trim().split(/\s+/).reverse().join(' ')
};


// 不使用内置 API
function reverseEachWord(s) {
    let chars = s.split("");
    let n = chars.length;
    let slow = 0, fast = 0;

    while (fast < n) {
        while (fast < n && chars[fast] != ' ') fast++;
        reverse(chars, slow, fast - 1);
        fast++;
        slow = fast;
    }

    return chars.join("");
}

function reverse(chars, left, right) {
    while (left < right) {
        let temp = chars[left];
        chars[left] = chars[right];
        chars[right] = temp;
        left++;
        right--;
    }
}

// 删除字符串中所有的空字符，但是单词之间需要一个空字符隔开
// 时间复杂度：O(n)
// 空间复杂度：O(1)，原地删除空字符
function trim(s) {
    let chars = s.split('');
    let n = chars.length;
    let slow = 0, fast = 0;

    while (fast < n) {
        if (chars[fast] !== ' ' || (fast >= 1 && chars[fast - 1] !== ' ')) {
            chars[slow] = chars[fast];
            slow++;
        }
        fast++;
    }

    if (slow >= 1 && chars[slow - 1] == ' ') slow--;

    chars.length = slow;
    return chars.join('');
}